package com.yfp.operation.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.operation.domain.OrderComboDO;
import com.yfp.operation.domain.dto.OrderComboGroupDTO;
import com.yfp.operation.domain.vo.SaleMarketInfoVO;
import com.yfp.operation.enums.OrderStatus;
import com.yfp.operation.mapper.OrderComboMapper;
import com.yfp.operation.service.OrderComboService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【order_combo】的数据库操作Service实现
 * @createDate 2024-04-19 16:48:13
 */
@Service
public class OrderComboServiceImpl extends ServiceImpl<OrderComboMapper, OrderComboDO> implements OrderComboService {

    @Resource
    private OrderComboMapper orderComboMapper;

    /**
     * 获取当天的订单
     *
     * @param machineId 设备id
     * @return 当天的订单列表
     */
    @Override
    public List<OrderComboDO> getTodayOrders(String machineId) {
        LambdaQueryWrapper<OrderComboDO> query = Wrappers.lambdaQuery(OrderComboDO.class);
        query.eq(OrderComboDO::getMachineId, machineId);
        // 创建日期为今天的订单
        DateTime dateTime = DateUtil.beginOfDay(DateUtil.date());
        DateTime dateTime2 = DateUtil.endOfDay(DateUtil.date());
        query.between(OrderComboDO::getCreateTime, dateTime, dateTime2);
        return this.list(query);
    }

    @Override
    public OrderComboDO getOrderComboByOrderId(String orderId) {
        return orderComboMapper.getOrderComboByOrderId(orderId);
    }

    @Override
    public List<String> getUserIds(String machineId) {
        return orderComboMapper.getUserIds(machineId);
    }

    @Override
    public List<String> getAgain(List<String> userIds) {
        return orderComboMapper.getAgain(userIds);
    }

    @Override
    public List<SaleMarketInfoVO> findByMachoineIdsPrice(List<String> machineIds) {
        return orderComboMapper.findByMachoineIdsPrice(machineIds);
    }

    @Override
    public List<OrderComboGroupDTO> listGroupOrderInfo(List<String> orderSn) {
        if (CollUtil.isEmpty(orderSn)){
            return CollUtil.newArrayList();
        }
        List<OrderComboDO> orderCombos = orderComboMapper.selectBatchIds(orderSn);
        // 封装数据
        List<OrderComboGroupDTO> orderComboGroupDTOList = orderCombos.stream().map(item -> {
            OrderComboGroupDTO orderComboGroupDTO = new OrderComboGroupDTO();
            orderComboGroupDTO.setOrderId(item.getOrderId());
            // 订单状态[1:未取餐、2:已取餐]
            if (OrderStatus.Finish.getStatus().equals(item.getStatus())) {
                orderComboGroupDTO.setStatus(2);
            } else {
                orderComboGroupDTO.setStatus(1);
            }
            return orderComboGroupDTO;
        }).collect(Collectors.toList());
        return orderComboGroupDTOList;
    }

    @Override
    public List<OrderComboDO> listByUserIds(List<String> clientIds) {
        if (CollUtil.isEmpty(clientIds)) {
            return List.of();
        }
        return orderComboMapper.listByUserIds(clientIds);
    }

    @Override
    public void editStatusByOrderId(String orderId, String status) {
        orderComboMapper.editStatusByOrderId(orderId, status);
    }
}




